<template>
  <el-card class="main-card">
    <div class="title">{{ this.$route.name }}</div>
    <div class="operation-container">
      <el-button type="primary" size="small" icon="el-icon-plus" @click="openModel(null)"> 新增 </el-button>
    </div>
    <el-table border :data="categories" @selection-change="selectionChange" v-loading="loading">
      <el-table-column prop="name" label="分类名" align="center" />
      <el-table-column prop="icon" label="图标" align="center" show-overflow-tooltip/>
      <el-table-column prop="attribute" label="额外属性" align="center" show-overflow-tooltip/>
      <el-table-column prop="isHidden" label="是否隐藏" align="center">
        <template slot-scope='scope'>
          <el-switch
            v-model='scope.row.isHidden'
            active-color='#13ce66'
            inactive-color='#F4F4F5'
            :active-value='1'
            :inactive-value='0'
            @change='changeDisable(scope.row)' />
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" align="center">
        <template slot-scope="scope">
          <i class="el-icon-time" style="margin-right: 5px" />
          {{ scope.row.createTime | date }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="160" align="center" >
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="openModel(scope.row)"> 编辑 </el-button>
          <el-popconfirm title="确定删除吗？" style="margin-left: 1rem" @confirm="deleteCategory(scope.row.id)">
            <el-button size="mini" type="danger" slot="reference"> 删除 </el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :visible.sync="isDelete" width="30%">
      <div class="dialog-title-container" slot="title"><i class="el-icon-warning" style="color: #ff9900" />提示</div>
      <div style="font-size: 1rem">是否删除选中项？</div>
      <div slot="footer">
        <el-button @click="isDelete = false">取 消</el-button>
        <el-button type="primary" @click="deleteCategory(null)"> 确 定 </el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="addOrEdit" width="30%">
      <div class="dialog-title-container" slot="title" ref="categoryTitle" />
      <el-form label-width="80px" size="medium" :model="categoryForm">
        <el-form-item label="分类名">
          <el-input v-model="categoryForm.name" style="width: 220px" />
        </el-form-item>
        <el-form-item label="图标">
          <el-input v-model="categoryForm.icon" style="width: 220px" />
        </el-form-item>
        <el-form-item label="额外属性">
          <el-input v-model="categoryForm.attribute" style="width: 220px" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="addOrEdit = false">取 消</el-button>
        <el-button type="primary" @click="addOrEditCategory"> 确 定 </el-button>
      </div>
    </el-dialog>
  </el-card>
</template>

<script>
export default {
  created() {
    this.listCategories()
  },
  data: function () {
    return {
      isDelete: false,
      loading: true,
      addOrEdit: false,
      keywords: null,
      categoryIds: [],
      categories: [],
      categoryForm: {
        id: null,
        name: '',
        isHidden: '',
        icon: '',
        attribute: ''
      },
      current: 1,
      size: 10,
      count: 0
    }
  },
  methods: {
    changeDisable(menu) {
      let params = {
        id: menu.id,
        isHidden: menu.isHidden
      }
      this.axios.put('/api/admin/blog/menus/hidden', params).then(({ data }) => {
        if (data.flag) {
          this.$notify.success({
            title: '成功',
            message: '修改成功'
          })
        } else {
          this.$notify.error({
            title: '失败',
            message: '修改失败'
          })
        }
      })
    },
    deleteCategory(id) {
      this.axios.delete('/api/admin/blog/menus/category/' + id).then(({ data }) => {
        if (data.flag) {
          this.$notify.success({
            title: '成功',
            message: '删除成功'
          })
          this.listCategories()
        } else {
          this.$notify.error({
            title: '失败',
            message: data.message
          })
        }
      })
    },
    listCategories() {
      this.axios
        .get('/api/admin/blog/menus/category/list')
        .then(({ data }) => {
          this.categories = data.data
          this.loading = false
        })
    },
    openModel(category) {
      if (category != null) {
        this.categoryForm = JSON.parse(JSON.stringify(category))
        this.$refs.categoryTitle.innerHTML = '修改分类'
      } else {
        this.categoryForm.id = null
        this.categoryForm.categoryName = ''
        this.$refs.categoryTitle.innerHTML = '添加分类'
      }
      this.addOrEdit = true
    },
    addOrEditCategory() {
      if (this.categoryForm.name.trim() === '') {
        this.$message.error('分类名不能为空')
        return false
      }
      this.axios.post('/api/admin/blog/menus/category', this.categoryForm).then(({ data }) => {
        if (data.flag) {
          this.$notify.success({
            title: '成功',
            message: data.message
          })
          this.listCategories()
        } else {
          this.$notify.error({
            title: '失败',
            message: data.message
          })
        }
        this.addOrEdit = false
      })
    }
  }
}
</script>
